// 蜂窝蜜造平台生成代码，如手工更改，请添加到 .beeignore 忽略生成

package com.fowo.api.service.impl.NavSellOrderLogisticsServiceImpl;

import com.amazon.spapi.model.ErrorList;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.fowo.api.model.nav.sell.order.NavSellOrderVo;
import com.fowo.api.model.nav.sell.order.SubmitOrderResult;
import com.fowo.api.service.NavSellOrderLogisticsService;
import com.fowo.api.syncAmazon.api.FbaOutboundApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * 订单管理 服务实现类
 *
 * @author 蜂窝蜜造平台 配置人：任凯
 */
@Service
@Slf4j
@DS("ds24")
public class NavSellOrderFbaLogisticsServiceImpl implements NavSellOrderLogisticsService {

    @Resource
    private FbaOutboundApiService fbaOutboundApiService;

    public String getType() {
        return "FBA";
    }

    /**
     * 物流下单
     *
     * @param navSellOrderVo
     */
    @Override
    public SubmitOrderResult submitLogisticsOrder(NavSellOrderVo navSellOrderVo) throws Exception {
        SubmitOrderResult submitOrderResult = new SubmitOrderResult();
        ErrorList errorList = fbaOutboundApiService.createFulfillmentOrder(navSellOrderVo);
        if (null != errorList) { //不为空表示有异常
            submitOrderResult.setSuccess(false);
            submitOrderResult.setCode(500);
            submitOrderResult.setMessage(errorList.getErrors().stream().map(e -> e.getMessage()).collect(Collectors.joining()));
        } else {
            submitOrderResult.setSuccess(true);
            submitOrderResult.setCode(200);
            submitOrderResult.setMessage("提交成功");
        }
        return submitOrderResult;
    }
}